﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using BLL.Article;


public partial class ArticleListing : System.Web.UI.UserControl
    {
        private bool _enableHighlighter = true;
        public bool EnableHighlighter
        {
            get { return _enableHighlighter; }
            set { _enableHighlighter = value; }
        }

        private bool _publishedOnly = true;
        public bool PublishedOnly
        {
            get { return _publishedOnly; }
            set
            {
                _publishedOnly = value;
                objArticles.SelectParameters["publishedOnly"].DefaultValue = value.ToString();
            }
        }

        private bool _showPageSizePicker = true;
        public bool ShowPageSizePicker
        {
            get { return _showPageSizePicker; }
            set
            {
                _showPageSizePicker = value;
                ddlArticlesPerPage.Visible = value;
                lblPageSizePicker.Visible = value;
            }
        }

        private bool _enablePaging = true;
        public bool EnablePaging
        {
            get { return _enablePaging; }
            set
            {
                _enablePaging = value;
                gvwArticles.PagerSettings.Visible = value;
            }
        }

        private bool _userCanEdit = false;
        protected bool UserCanEdit
        {
            get { return _userCanEdit; }
            set { _userCanEdit = value; }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            this.Page.RegisterRequiresControlState(this);

            this.UserCanEdit = (this.Page.User.Identity.IsAuthenticated &&
               (this.Page.User.IsInRole("Administrators") || this.Page.User.IsInRole("Editors")));
        }

        protected void Page_Load(object sender, EventArgs e)
      {
            if (!this.IsPostBack)
            {
                // preselect the category whose ID is passed in the querystring
                if (!string.IsNullOrEmpty(this.Request.QueryString["CatID"]))
                {

                }

                // Set the page size as indicated in the config file. If an option for that size
                // doesn't already exist, first create and then select it.
                int pageSize = Globals.Settings.Articles.PageSize;
                if (ddlArticlesPerPage.Items.FindByValue(pageSize.ToString()) == null)
                    ddlArticlesPerPage.Items.Add(new ListItem(pageSize.ToString(), pageSize.ToString()));
                ddlArticlesPerPage.SelectedValue = pageSize.ToString();
                gvwArticles.PageSize = pageSize;

                gvwArticles.DataBind();
            }
        }

        protected void ddlArticlesPerPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvwArticles.PageSize = int.Parse(ddlArticlesPerPage.SelectedValue);
            gvwArticles.PageIndex = 0;
            gvwArticles.DataBind();
        }

        protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvwArticles.PageIndex = 0;
            gvwArticles.DataBind();
        }

        protected void gvwArticles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Approve")
            {
                int articleID = int.Parse(e.CommandArgument.ToString());
                Article.ApproveArticle(articleID);
                gvwArticles.PageIndex = 0;
                gvwArticles.DataBind();
            }
        }
    }
